package org.tulkas.webclient;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author <a href="mailto:tulkas.web@gmail.com">Tulkas</a>
 *
 */
public class WebClient {
	public static void main(String[] args) throws Exception {
		InputStream is = new FileInputStream(
				"D:/devProjects/Tulkas/workspace/tulkas-browser/src/test/resources/org/tulkas/robot/res/bak_0.file");
		byte[] bytes = readBytes(is);
		long cur = System.currentTimeMillis();
		String ss = new String(bytes);
		for (int i = 0; i < 100000; i++) {
			ss.indexOf("$TerminatingClass");
		}
		System.out.println(System.currentTimeMillis() - cur);
	}

	public static byte[] readBytes(InputStream in) throws IOException {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		readAndWrite(in, out);
		byte[] bytes = out.toByteArray();
		out.close();
		return bytes;
	}

	public static void readAndWrite(InputStream in, OutputStream out)
			throws IOException {
		byte[] buffer = new byte[1024];
		int read;
		while (true) {
			read = in.read(buffer);
			if (read <= 0)
				break;
			out.write(buffer, 0, read);
		}
	}
}
